package com.auto.service.impl.coder.tree.java;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auto.annotation.Column;
import com.auto.annotation.MyFunction;
import com.auto.annotation.Table;
import com.auto.service.impl.BaseCoder;
import com.auto.service.impl.Coder;
import com.common.ArrayUtils;
import com.common.CommonUtil;
import com.common.Global;

/**
 * Controller的代码自动生成
 * @author chenfuqiang
 *
 */
public class ControllerCoder extends BaseCoder implements Coder {
	
	private final String CLASSTAIL = "Controller.java";
	private final String TEMPLATE_PATH = "/tree/java/Controller.ftl";
	
	
	@Override
	public Map<String, Object> getRoot(Class<?> pojo) {
		Map<String,Object> root = new HashMap<String, Object>();
		root.put("showWay","TREE");
		root.put("package", Global.CONTROLLER_PACKAGE);
		root.put("beanPackage", Global.PACKAGEOFTARGETPOJO);
		root.put("daoPackage", Global.DAO_PACKAGE);
		root.put("servicePackage", Global.SERVICE_PACKAGE);
		root.put("author", Global.AUTHOR);
		root.put("className", pojo.getSimpleName());
		root.put("paramName", pojo.getSimpleName().substring(0,1).toLowerCase()+pojo.getSimpleName().substring(1));
		Table table = pojo.getAnnotation(Table.class);
		String tableName = table.name();
		if(CommonUtil.isEmpty(tableName)) {
			tableName = pojo.getSimpleName();
		}
		//功能扩展
		MyFunction myFunction = pojo.getAnnotation(MyFunction.class);
		if(myFunction != null) {
			root.put("autoInitFromAccess", myFunction.autoInitFromAccess());
			root.put("copyLastYearData", myFunction.copyLastYearData());
			root.put("treeSort", myFunction.treeSort());
			root.put("importData", myFunction.importData());
		}else {
			root.put("autoInitFromAccess", false);
			root.put("copyLastYearData", false);
			root.put("treeSort", false);
			root.put("importData", false);
		}
		root.put("jsname", table.jsname());
		Field[] fields = pojo.getDeclaredFields();
		if(pojo.getSuperclass()!=null){
			Class<?> clsSup = pojo.getSuperclass();
			fields = (Field[]) ArrayUtils.addAll(fields,clsSup.getDeclaredFields());
		}
		String key = null;
		List<Map<String,String>> beans = new ArrayList<Map<String,String>>();
		for(Field field : fields) {
			Column column = field.getAnnotation(Column.class);
			if(column != null) {
				if(column.flag().toLowerCase().equals("primary")) {
					key = field.getName();
				}
				if(CommonUtil.isEmpty(column.jsname()) &&!column.flag().toLowerCase().equals("primary")) {
					continue;
				}
				
				String decimalPrecision = null;
				String proper = field.getName();
				String conf = null;
				
				Class<?> type = field.getType();
				
				String xx = type.getSimpleName().toUpperCase();
				
				Map<String,String> map = new HashMap<String, String>();
				map.put("dataIndex", proper);
				map.put("setMethodName", "set"+proper.substring(0,1).toUpperCase()+proper.substring(1));
				map.put("jsname", column.jsname());
				map.put("allowBlank", String.valueOf(column.jsAllowBlank()));
				map.put("type", xx);
				map.put("conf", CommonUtil.nullToStr(conf));
				map.put("decimalPrecision", decimalPrecision);
				beans.add(map);
			}
		}
		root.put("beans", beans);
		root.put("key",key);
		return root;
	}
	
	@Override
	public String getTargetPackage() {
		return Global.CONTROLLER_PACKAGE;
	}

	@Override
	public String getSrcPath() {
		return Global.SRCPATH;
	}

	@Override
	public String getFtlPath() {
		return this.TEMPLATE_PATH;
	}

	@Override
	public String getClassTail() {
		return this.CLASSTAIL;
	}

	

}
